<?php
/*$conn = mysqli_connect("localhost","root","","bbs") or die(""数据库连接错误);
mysqli_query($conn,'set names utf8');*/
//使用PDO连接数据库
class Bbs{  //定义一个类，名字叫bbs
    private $conn;//定义一个私有变量，名称是conn
    function __construct()  //构造函数
    {
        try{
            $this->conn = new PDO("mysql:host=localhost;dbname=bbs","root","");
            $this->conn->query("set names utf8");
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (PDOException $e){
            die('数据库连接出错！'.$e->getMessage());
        }
    }
    function __destruct() // 析构函数
    {
        // TODO: Implement __destruct() method.
    }
    function login($username,$pw){
        try {
            $sql = "select * from userinfo where username = ? and pw = ?";
            $stm = $this->conn->prepare($sql);
            $stm->execute(array($username, md5($pw)));
            $rowNum = $stm->rowCount();
            $a['error'] = 0;
            $a['rowNum'] = $rowNum;
            if($rowNum){
                $a['info'] = $stm->fetch();
            }
            return $a;
        }
        catch (PDOException $e){
            $a['error'] = 1;
            $a['errMsg'] = '数据查询出错';
            return $a;
        }
    }
    function query($sql,$action = 1){
        //$action:1表示要取内容（默认），0表示只取数量
        $result = $this ->conn->query($sql);
        if($this->conn->errorCode() != "00000"){
            $a['error'] = 1;
        }
        else{
            $a['error'] = 0;
            if($action){
                $a['result'] = $result -> fetchAll(PDO::FETCH_ASSOC);
            }
            else{
                $a['total'] = $result -> rowCount();
            }
        }
        return $a;
    }
    function update($table,$action,$con,$value="",$col="")
        //如果action=1表明，是更新点击次数。否则，是更新value
    {
        try {
            if($action){
                $sql = "update $table set hits = hits + 1 where $con";
                $this->conn->query($sql);
            }
            else{
                $sql = "update $table set $col = ?  where $con";
                $stm = $this->conn->prepare($sql);
                $stm->execute(array($value));
                $rowCount = $stm->rowCount();
                $a['error'] = 0;
                $a['rowCount'] = $rowCount;
                return $a;
            }
        } catch (PDOException $e) {
            $a['error'] = 1;
            $a['errMsg'] = '数据更新出错';
            $a['errMsg1'] = $e->getMessage();
            return $a;
        }
    }
    function anyQuery($table,$col,$value){
        try{
            $sql = "select * from $table where $col = ? order by id desc";
            $stm = $this->conn->prepare($sql);
            $stm->execute(array($value));
            $rowCount = $stm->rowCount();
            //return $stm;
            $a['error'] = 0;
            $a['rowCount'] = $rowCount;
            if($rowCount){
                $a['info'] = $stm->fetchAll();
            }
            return $a;
        }
        catch (PDOException $e){
            $a['error'] = 1;
            $a['errMsg'] = '数据查询出错';
            $a['errMsg1'] = $e->getMessage();
            return $a;
        }
    }
    function singup($fileName,$username,$pw,$nickname,$birthday,$email){
        try {
            $sql = "select * from userinfo where username = ? ";
            $stm = $this->conn->prepare($sql);
            $stm->execute(array($username));
            $rowNum = $stm->rowCount();
            if($rowNum){
                $a['error'] = 1;
                $a['errMsg'] = '此用户已经存在';
            }
            else{
                $sql = "insert into userinfo set username = ?,pw = ?,nickname = ?,birthday=?,email=?,pic=?,postdate=? ";
                $stm = $this->conn->prepare($sql);
                $stm->execute(array($username,md5($pw),$nickname,$birthday,$email,$fileName,date("Y-m-d H:i:s")));
                $rowNum = $stm->rowCount();
                if($rowNum){
                    $a['error'] = 0;
                }
                else{
                    $a['error'] = 1;
                    $a['errMsg'] = '写入数据库失败';
                }
            }
            return $a;
        }
        catch (PDOException $e){
            $a['error'] = 1;
            $a['errMsg'] = '数据查询出错';
            return $a;
        }
    }
    function getInfo($username,$action){  //如果action=1，表示依据用户名查询，否则，依据id查询
        try {
            if($action){
                $sql = "select * from userinfo where username = ? ";
            }
            else{
                $sql = "select * from userinfo where id = ? ";
            }
            $stm = $this->conn->prepare($sql);
            $stm->execute(array($username));
            $rowNum = $stm->rowCount();
            $a['error'] = 0;
            $a['rowNum'] = $rowNum;
            if($rowNum){
                $a['info'] = $stm->fetch();
            }
            return $a;
        }
        catch (PDOException $e){
            $a['error'] = 1;
            $a['errMsg'] = '数据查询出错';
            return $a;
        }
    }
    function postMainTopic($data,$title,$uid){
        $sql = "insert into maintopic (title,contents,userid,posttime) 
values ('$title','$data','".$uid."','".date("Y-m-d H:i:s")."')";
        $this->conn->query($sql);
        if($this->conn->errorCode() != "00000"){
            return 0;
        }
        else{
            return 1;
        }
    }
    function topic($table,$uid){
        try{
            $sql = "select * from $table where userid = ? order by id desc";
            $stm = $this->conn->prepare($sql);
            $stm->execute(array($uid));
            $rowCount = $stm->rowCount();
            $a['error'] = 0;
            $a['rowCount'] = $rowCount;
            if($rowCount){
                $a['info'] = $stm->fetch();
            }
            return $a;
        }
        catch (PDOException $e){
            $a['error'] = 1;
            $a['errMsg'] = '数据查询出错';
            return $a;
        }
    }
    function reply($mainTopicID,$contents,$quotedid){
        $sql = "insert into subtopic set mainTopicID = ?,userid = ?,quoteid = ?,posttime = ?,contents = ?";
        try{
            $stm = $this->conn->prepare($sql);
            if($quotedid == 'null') $quotedid = NULL;
            $stm->execute(array($mainTopicID,$_SESSION['loginUserID'],$quotedid,date("Y-m-d H:i:s"),$contents));
            $rowCount = $stm->rowCount();
            $a['error'] = 0;
            $a['rowCount'] = $rowCount;
            return $a;
        }
        catch (PDOException $e){
            $a['error'] = 1;
            $a['errMsg'] = $e;//'数据插入出错';
            return $a;
        }
    }
}
